package org.jash.demo04

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.jash.comment.utils.service
import org.jash.demo04.entity.RecommendInfo

class MainViewModel: ViewModel() {
    private val _data: MutableLiveData<List<RecommendInfo>> = MutableLiveData()
    val data: LiveData<List<RecommendInfo>>
        get() = _data
    private val service: APIService by lazy { service() }
    fun load() {
        viewModelScope.launch {
            try {
                val res = service.guessYouLike()
                if (res.ret == 200) {
                    _data.postValue(res.data.recommendInfoList)
                } else {
                    Log.d("MainViewModel", "load: 请求失败, 原因: ${res.msg}")
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
    }

}